package com.witmed.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.witmed.entity.Appointment;
import com.witmed.entity.Department;
import com.witmed.entity.Doctor;
import com.witmed.entity.Hospital;
import com.witmed.entity.UserInfo;
import com.witmed.service.AppointmentOperationService;
import com.witmed.service.impl.AppointmentOperationServiceImpl;
import com.witmed.util.PageInfo;
 
/**
 * Servlet implementation class AppointmentOperationServlet
 */
@WebServlet("/AppointmentOperationServlet")
public class AppointmentOperationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AppointmentOperationService appointmentOperationService =new AppointmentOperationServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AppointmentOperationServlet() {
        super();
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    String op = request.getParameter("op");//获取操作符
		if ("getDoctorListLike".equals(op)) {
	    	doGetDoctorlistsearchByDoctorNameOrDepatmentNameOrhosptialName(request,response);
		}else if ("findAllHospital".equals(op)) {
			getHospitalList(request,response);
		}else if ("findAllDepartment".equals(op)) {
			getDepratmentList(request, response);
		}else if ("seachByHospitalName".equals(op)) {
			doGetDoctorlistsearchByHosptialNameOrDepatmentNameOrDoctor(request,response);
		}else if ("seachByDepartmentName".equals(op)){
			doGetDoctorlistsearchByHosptialNameOrDepatmentNameOrDoctor(request,response);
		}else if ("seachByAppointmentTime".equals(op)) {
			doSeachByAppointmentTime(request,response);
		}else if ("confirmAppiontment".equals(op)) {
			doConfirmAppiontment(request,response);
		}else if ("inserAppointmentOperationTime".equals(op)) {
			doInserAppointmentOperationTime(request,response);
		}else if ("getAppointmentTime".equals(op)) {
			doAppointmentTime(request,response);
		}else if ("deleteAppointmentTimeId".equals(op)) {
			doDeleteAppointmentTimeId(request,response);
		}
		
	}
	
	
	/**
	 * 医生删除预约时间
	 * @param request
	 * @param response
	 * @throws IOException 
	 */

	private void doDeleteAppointmentTimeId(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Integer appointmentTimeId = Integer.parseInt(request.getParameter("appointmentTimeId"));
		boolean b = appointmentOperationService.delete(appointmentTimeId);
		if (b) {
			ObjectMapper objectMapper =new ObjectMapper();
			String json = objectMapper.writeValueAsString(b);
			//将数据响应回前端
			PrintWriter out = response.getWriter();
			out.print(json);
		}
	}

	/**
	 * 医生修改预约时间
	 * @param 侯仁杰
	 * @param response
	 * @throws IOException 
	 */
	private void doAppointmentTime(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String appointmentTime = request.getParameter("appointmentTime");
		String appointmenttimeId = request.getParameter("appointmenttimeId");
		Appointment appointment = new Appointment(Integer.parseInt(appointmenttimeId), appointmentTime, null, null);
		boolean list = appointmentOperationService.updateDoctor(appointment);
		System.out.println(list);
		if (list) {
			ObjectMapper objectMapper =new ObjectMapper();
			String json = objectMapper.writeValueAsString(list);
			//将数据响应回前端
			PrintWriter out = response.getWriter();
			out.print(json);
		}
	}

	/**
	 * 新增预约时间
	 * @param 仁杰
	 * @param response
	 * @throws IOException 
	 */
	private void doInserAppointmentOperationTime(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String time = request.getParameter("time");
		String doctorId = request.getParameter("doctorId");
		Doctor doctor = new Doctor(Integer.parseInt(doctorId), null, null, null, null, null, null, null);
		Appointment appointment = new Appointment(null, time, doctor, null);
		boolean list = appointmentOperationService.insert(appointment);
		if (list) {
			 // 响应数据
		    PrintWriter  out= response.getWriter();
		    out.print("success");
		}
	}

	/**
	 * 预约时间确认
	 * ChenYouming
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void doConfirmAppiontment(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json;charset=utf-8");
		String id = request.getParameter("id");
		String time = request.getParameter("time");
		String userId = request.getParameter("userid");
		UserInfo user =new UserInfo(Integer.parseInt(userId), null, null, null, null, null, null, null, null);
		Appointment appointment =new Appointment(Integer.parseInt(id), time, null, user);
		//调用业务层，获取数据库数据
		 boolean list = appointmentOperationService.update(appointment);
		 System.out.println(list);
		 String success;
		 if (list==true) {
			  success ="success";
		}else {
			success="error";
		}
		//将java对象转成json格式
			ObjectMapper objectMapper =new ObjectMapper();
			String json = objectMapper.writeValueAsString(success);
			//将数据响应回前端
			PrintWriter out = response.getWriter();
			out.print(json);
	}

	/**
	 * 查询预约时间表
	 * ChenYouming
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	
	private void doSeachByAppointmentTime(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json;charset=utf-8");
		String time = request.getParameter("time");
		String doctorId = request.getParameter("doctorId");
		//调用业务层，获取数据库数据
		 List<Appointment> list = appointmentOperationService.findAppointment(time, Integer.parseInt(doctorId));
		 //将java对象转成json格式
		ObjectMapper objectMapper =new ObjectMapper();
		String json = objectMapper.writeValueAsString(list);
		//将数据响应回前端
		PrintWriter out = response.getWriter();
		out.print(json);
	}

	/**
	 * 查询所有科室
	 * ChenYouming
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
 private void getDepratmentList(HttpServletRequest request, HttpServletResponse response) throws IOException {
	response.setContentType("application/json;charset=utf-8");
	//调用业务层，获取数据库数据
	 List<Department> list = appointmentOperationService.findDepartment();
	//将java对象转成json格式
	ObjectMapper objectMapper =new ObjectMapper();
	String json = objectMapper.writeValueAsString(list);
	//将数据响应回前端
	PrintWriter out = response.getWriter();
	out.print(json);
		
	}

/**
 * 查询所有医院.
 * ChenYouming
 * @param request
 * @param response
 * @throws IOException 
 */
	private void getHospitalList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json;charset=utf-8");
		//调用业务层，获取数据库数据
		 List<Hospital> list = appointmentOperationService.findHospital();
		//将java对象转成json格式
		ObjectMapper objectMapper =new ObjectMapper();
		String json = objectMapper.writeValueAsString(list);
		//将数据响应回前端
		PrintWriter out = response.getWriter();
		out.print(json);
		
	}

	
	/**
	 * 通过选择条件搜索查询
	 * ChenYouming
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void doGetDoctorlistsearchByHosptialNameOrDepatmentNameOrDoctor(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
				response.setContentType("application/json;charset=utf-8");
				//获取前端的数据
				String pageNum1 = request.getParameter("pageNum");
				int pageNum = Integer.parseInt(pageNum1);
				String pageSize1 =request.getParameter("pageSize");
				int pageSize = Integer.parseInt(pageSize1);
				String hospitalName = request.getParameter("hospitalName");
				String departmentName = request.getParameter("departmentName");
				Map<String ,Object> map =new HashMap<String ,Object>();
				map.put("pageNum", pageNum);
				map.put("pageSize", pageSize);
				map.put("hospitalName", hospitalName);
				map.put("departmentName", departmentName);
				//调用业务层，获取数据库数据
				PageInfo<Doctor> pageInfo = appointmentOperationService.searchByhosptialNameOrDepatmentNameOrDoctorNameAll(map);
				//将java对象转成json格式
				ObjectMapper objectMapper =new ObjectMapper();
				String json = objectMapper.writeValueAsString(pageInfo);
				//将数据响应回前端
				PrintWriter out = response.getWriter();
				out.print(json);
		
	}
	/**
	 * 通过医生姓名或医院名字或部门名字的关键字进行分页查询
	 * ChenYouming
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void doGetDoctorlistsearchByDoctorNameOrDepatmentNameOrhosptialName(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		//获取前端的数据
				response.setContentType("application/json;charset=utf-8");
				String typeId1 = request.getParameter("typeId");
				int typeId = Integer.parseInt(typeId1);
				String keyword = request.getParameter("keyWord");
				String pageNum1 = request.getParameter("pageNum");
				int pageNum = Integer.parseInt(pageNum1);
				String pageSize1 =request.getParameter("pageSize");
				int pageSize = Integer.parseInt(pageSize1);
				//调用业务层，获取数据库数据
				PageInfo<Doctor> pageInfo = appointmentOperationService.searchByDoctorNameOrDepatmentNameOrhosptialName(typeId, keyword, pageNum, pageSize);
				//将java对象转成json格式
				ObjectMapper objectMapper =new ObjectMapper();
				String json = objectMapper.writeValueAsString(pageInfo);
				//将数据响应回前端
				PrintWriter out = response.getWriter();
				out.print(json);
		
	}
}
